package leetcode.median;

import java.util.List;

/**
 * ClassName: $120_MinimumTotal
 * Description:
 * date: 2021-11-13 16:36
 *
 * @author liyifan
 */
public class $120_MinimumTotal {
    public int minimumTotal(List<List<Integer>> triangle) {
        int m = triangle.size();
        int[][] dp = new int[m][m];
        dp[0][0] = triangle.get(0).get(0);

        for (int i = 1;i < m;i++) {
            for (int j = 0;j < i;j++){
                if (j == 0) {
                    dp[i][j] = dp[i - 1][0] + triangle.get(i).get(j);
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle.get(i).get(j);
                }
            }
            dp[i][i] = dp[i - 1][i - 1] + triangle.get(i).get(i);
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0;i < m;i++){
            min = Math.min(min, dp[m - 1][i]);
        }
        return min;
    }
}
